from pydantic import BaseModel, Field, validator
from typing import Optional, List
from datetime import datetime


class ClockRecordRuleTime(BaseModel):
    after_time: Optional[int] = Field(alias="afterTime", default=None)
    before_time: Optional[int] = Field(alias="beforeTime", default=None)
    clock_in_time: Optional[str] = Field(alias="clockInTime", default=None)
    index: Optional[int] = None
    next_day: Optional[bool] = Field(alias="nextDay", default=None)


class ClockRecord(BaseModel):
    next_day: Optional[bool] = Field(alias="nextDay", default=None)
    punch_in_lat: Optional[str] = Field(alias="punchInLat", default=None)
    punch_in_lnt: Optional[str] = Field(alias="punchInLnt", default=None)
    punch_in_time: Optional[str] = Field(alias="punchInTime", default=None)
    rule_time: Optional[str] = Field(alias="ruleTime", default=None)
    state: Optional[str] = None


class ClockRuleFencePoints(BaseModel):
    latitude: Optional[float] = None
    longitude: Optional[float] = None


class ClockRuleFence(BaseModel):
    id: Optional[int] = None
    lat: Optional[str] = None
    lng: Optional[str] = None
    name: Optional[str] = None
    points: Optional[List[ClockRuleFencePoints]] = None
    radius: Optional[int] = None
    type: Optional[int] = None


class ClockRule(BaseModel):
    area_name: Optional[str] = Field(alias="areaName", default=None)
    id: Optional[int] = None
    next_day: Optional[bool] = Field(alias="nextDay", default=None)
    rule_name: Optional[str] = Field(alias="ruleName", default=None)
    fence: Optional[ClockRuleFence] = None

    # 日期时间自动转换
    dt: Optional[datetime] = None

    @validator('dt', pre=True)
    def parse_dt(cls, value):
        if isinstance(value, str):
            return datetime.fromisoformat(value.replace('Z', '+00:00'))
        return value


class PClockRecord(BaseModel):
    _class: Optional[str] = Field(alias="_class", default=None)
    attendance_group_name: Optional[str] = Field(alias="attendanceGroupName", default=None)
    attendance_shift_size: Optional[int] = Field(alias="attendanceShiftSize", default=None)
    clock_record: Optional[ClockRecord] = Field(alias="clockRecord", default=None)
    clock_rule: Optional[ClockRule] = Field(alias="clockRule", default=None)
    clock_rule_name: Optional[str] = Field(alias="clockRuleName", default=None)
    depart: Optional[int] = None
    depart_name: Optional[str] = Field(alias="departName", default=None)
    dt: Optional[datetime] = None
    id: Optional[int] = None
    imei: Optional[str] = None
    job_post: Optional[str] = Field(alias="jobPost", default=None)
    nick_name: Optional[str] = Field(alias="nickName", default=None)
    project_id: Optional[int] = Field(alias="projectId", default=None)
    region_id: Optional[int] = Field(alias="regionId", default=None)
    station_step_id: Optional[int] = Field(alias="stationStepId", default=None)
    user_name: Optional[str] = Field(alias="userName", default=None)
    valid_state: Optional[int] = Field(alias="validState", default=None)

    # 关键字段验证
    @validator('id', 'depart', pre=True)
    def check_required_fields(cls, value):
        if value is None:
            raise ValueError("关键字段不能为空")
        return value